#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <QDebug>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    m_loginForm = new LoginForm(ui->widget);
    m_envForm = new IkeaEnvForm(ui->widget);
    m_devForm = new HomeDevForm(ui->widget);
    m_dataMSForm = new DataMSForm(ui->widget);
    m_userMSForm = new UserMSForm(ui->widget);
    m_personForm = new PersonInfoForm(ui->widget);

    connect(m_loginForm, SIGNAL(signalUserLogin(QString,QString)),
            this, SLOT(slotUserLogin(QString,QString)));

    m_loginForm->show();
    m_envForm->hide();
    m_devForm->hide();
    m_dataMSForm->hide();
    m_userMSForm->hide();
    m_personForm->hide();

    ui->mainToolBar->hide();
    ui->menuBar->hide();
    ui->statusBar->hide();
    this->setContextMenuPolicy(Qt::NoContextMenu); //去掉工具栏的右键菜单
    this->setCentralWidget(ui->widget);

    m_msgServer = new MsgServer;
}

MainWindow::~MainWindow()
{
    delete m_msgServer;
    delete ui;
}

void MainWindow::slotUserLogin(QString id, QString pswd)
{
    if((id == "admin") && (pswd == "admin"))
    {
        m_loginForm->hide();
        ui->mainToolBar->show();
        on_actionEnv_triggered();
    }else
    {
        m_loginForm->userLoginFail();
    }
}

void MainWindow::on_actionEnv_triggered()
{
    ui->actionEnv->setEnabled(false);
    ui->actionDev->setEnabled(true);
    ui->actionData->setEnabled(true);
    ui->actionUser->setEnabled(true);
    ui->actionInfo->setEnabled(true);

    m_envForm->show();
    m_devForm->hide();
    m_dataMSForm->hide();
    m_userMSForm->hide();
    m_personForm->hide();
}

void MainWindow::on_actionDev_triggered()
{
    ui->actionEnv->setEnabled(true);
    ui->actionDev->setEnabled(false);
    ui->actionData->setEnabled(true);
    ui->actionUser->setEnabled(true);
    ui->actionInfo->setEnabled(true);

    m_envForm->hide();
    m_devForm->show();
    m_dataMSForm->hide();
    m_userMSForm->hide();
    m_personForm->hide();
}

void MainWindow::on_actionData_triggered()
{
    ui->actionEnv->setEnabled(true);
    ui->actionDev->setEnabled(true);
    ui->actionData->setEnabled(false);
    ui->actionUser->setEnabled(true);
    ui->actionInfo->setEnabled(true);

    m_envForm->hide();
    m_devForm->hide();
    m_dataMSForm->show();
    m_userMSForm->hide();
    m_personForm->hide();
}

void MainWindow::on_actionUser_triggered()
{
    ui->actionEnv->setEnabled(true);
    ui->actionDev->setEnabled(true);
    ui->actionData->setEnabled(true);
    ui->actionUser->setEnabled(false);
    ui->actionInfo->setEnabled(true);

    m_envForm->hide();
    m_devForm->hide();
    m_dataMSForm->hide();
    m_userMSForm->show();
    m_personForm->hide();
}

void MainWindow::on_actionInfo_triggered()
{
    ui->actionEnv->setEnabled(true);
    ui->actionDev->setEnabled(true);
    ui->actionData->setEnabled(true);
    ui->actionUser->setEnabled(true);
    ui->actionInfo->setEnabled(false);

    m_envForm->hide();
    m_devForm->hide();
    m_dataMSForm->hide();
    m_userMSForm->hide();
    m_personForm->show();
}
